
package org.moyakarta.rest;

import java.net.URI;
import java.util.Collection;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("places")
public class PlaceService
{
   @javax.inject.Inject
   private PlaceStorage placeStorage;

   @Path("{id}")
   @GET
   @Produces("application/json")
   public Place get(@PathParam("id") String id) throws PlaceNotFoundException
   {
      Place place = placeStorage.getPlace(id);
      if (place == null)
         throw new PlaceNotFoundException(id);
      return place;
   }

   @GET
   @Produces("application/json")
   public Collection<Place> getAll()
   {
      return placeStorage.getAll();
   }

   @PUT
   @Consumes("application/json")
   public Response put(Place place, @Context UriInfo uriInfo)
   {
      String id = placeStorage.putPlace(place);
      URI location = uriInfo.getBaseUriBuilder().path(getClass()).path(id).build();
      return Response.created(location).entity(location.toString()).type("text/plain").build();
   }
}
